import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { getCategorySimpleList } from '#/api/bpm/category';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.bpm.task.common.taskName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.bpm.task.common.taskNamePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'processDefinitionId',
      label: $t('views.bpm.task.common.processDefinitionId'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.bpm.task.common.processDefinitionIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'category',
      label: $t('views.bpm.task.common.processCategory'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.bpm.task.common.processCategoryPlaceholder'),
        allowClear: true,
        api: getCategorySimpleList,
        labelField: 'name',
        valueField: 'code',
      },
    },
    {
      fieldName: 'status',
      label: $t('views.bpm.task.common.processStatus'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(
          DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS,
          'number',
        ),
        placeholder: $t('views.bpm.task.common.processStatusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('views.bpm.task.common.processStartTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'processInstance.name',
      title: $t('views.bpm.task.common.processName'),
      minWidth: 200,
      fixed: 'left',
    },
    {
      field: 'processInstance.summary',
      title: $t('views.bpm.task.common.summary'),
      minWidth: 200,
      formatter: ({ cellValue }) => {
        return cellValue && cellValue.length > 0
          ? cellValue
              .map((item: any) => `${item.key} : ${item.value}`)
              .join('\n')
          : '-';
      },
    },
    {
      field: 'processInstance.startUser.nickname',
      title: $t('views.bpm.task.common.startUser'),
      minWidth: 120,
    },
    {
      field: 'name',
      title: $t('views.bpm.task.common.currentTask'),
      minWidth: 180,
    },
    {
      field: 'createTime',
      title: $t('views.bpm.task.todo.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'processInstanceId',
      title: $t('views.bpm.task.common.processInstanceId'),
      minWidth: 280,
    },
    {
      field: 'id',
      title: $t('views.bpm.task.common.taskId'),
      minWidth: 280,
    },
    {
      title: $t('common.action'),
      width: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
